package com.se7en.ufutil;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
public class MainViewController extends MapActivity {
	private StudyArea studyArea;
	private FloorPlan floorPlan;
	private Trash trash;
	private Dining dining;
	private DataAccess dataAccess;
	private Parking parking;
	private Overlay activeOverlay;
	private LocationManager locationManager;
	private LocationListener locationListener;
	private MapView theMapView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Set up map view and location manager/listener
        theMapView = (MapView) findViewById(R.id.themap);
        theMapView.setBuiltInZoomControls(true);
        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        locationListener = initLocationListener();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
        
        //Create singleton objects
        dataAccess = new DataAccess();
        studyArea = new StudyArea(dataAccess);
        parking = new Parking();
        dining = new Dining();
        trash = new Trash(theMapView.getContext(), dataAccess);
        floorPlan = new FloorPlan();
        theMapView.getOverlays().add(trash);
        //Adlai: Initial active module? Load from file? Choose default? None?
    }
    // Adlai: This function is required to be implemented in order to conform to the Google Maps TOS
    // We don't ever display directions, so this can just always be false.
    public boolean isRouteDisplayed()
    {
    	return false;
    }
    private LocationListener initLocationListener()
    {
    	final MapController control = theMapView.getController();
        LocationListener listener = new LocationListener() {
        	public void onLocationChanged(Location location) {
				control.setCenter(new GeoPoint((int)location.getLatitude(),(int)location.getLongitude()));
			}
			public void onProviderDisabled(String arg0) {
				// TODO Auto-generated method stub
				
			}
			public void onProviderEnabled(String arg0) {
				// TODO Auto-generated method stub
				
			}
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
				// TODO Auto-generated method stub
				
			}
        };
        return listener;
    }
    public void changeOverlay(Overlay newOverlay)
    {
    	if(activeOverlay == newOverlay) return;
    	theMapView.getOverlays().remove(activeOverlay);
    	activeOverlay = newOverlay;
    	theMapView.getOverlays().add(activeOverlay);
    }
}